package com.EAIPClub.codeidemanager.service.impl;

import com.EAIPClub.codeidemanager.mapper.CodeIdeManagerMapper;
import com.EAIPClub.codeidemanager.service.CodeIdeManagerService;
import com.api.ConstantPool;
import com.api.POJO.CodeLab.CodeIdeManager.CodeIdeAccount;
import com.api.POJO.CodeLab.CodeIdeManager.CodeIdeManagerReturnMsg;
import com.api.POJO.CodeLab.CodeIdeManager.CodeIdeUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :CodeIde管理模块业务逻辑层实现
 * @Author :崔昌赫
 * @Date : 2021/10/26
 */
@Service
@Slf4j
public class CodeIdeManagerServiceImpl implements CodeIdeManagerService {
    private CodeIdeManagerMapper mapper;

    @Autowired
    public void setMapper(CodeIdeManagerMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public CodeIdeManagerReturnMsg setCodeIdePassword(CodeIdeAccount account) {
        log.info("即将添加codeide密码.account:{}",account);
        CodeIdeManagerReturnMsg returnMsg = new CodeIdeManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        Boolean returnbol = mapper.addNewCodeIdePassword(account);
        if(returnbol==null || !returnbol){
            log.warn("添加codeide密码失败!返回值:{}",returnbol);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("添加codeide密码失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("添加codeide密码成功!account:{}",account);
        returnMsg.setMsg(Msg);
        return  returnMsg;
    }

    @Override
    public CodeIdeManagerReturnMsg setCodeIdeUserInfo(CodeIdeUserInfo info) {
        log.info("即将添加codeide用户容器信息.info:{}",info);
        CodeIdeManagerReturnMsg returnMsg = new CodeIdeManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        Boolean returnbol = mapper.addNewCodeIdeUserInfo(info);
        if (returnbol==null || !returnbol){
            log.warn("添加codeide用户容器信息失败!返回值:{}",returnbol);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("添加codeide用户容器信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("添加codeide用户容器成功!info:{}",info);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public CodeIdeManagerReturnMsg getCodeIdePasswordByUid(String uid) {
        log.info("即将开始及通过uid查询codeide密码.uid:{}",uid);
        CodeIdeManagerReturnMsg returnMsg = new CodeIdeManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        CodeIdeAccount codeIdePasswordByUid = mapper.findCodeIdePasswordByUid(uid);
        if (codeIdePasswordByUid==null){
            log.warn("通过uid查询codeide密码失败!uid:{}",uid);
            Msg.add("通过uid查询codeide密码失败!");
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("查询到用户信息:{}",codeIdePasswordByUid);
        returnMsg.setAccount(codeIdePasswordByUid);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public CodeIdeManagerReturnMsg getCodeIdeUserInfoByUid(String uid) {
        log.info("即将开始及通过uid查询用户coodeide信息.uid:{}",uid);
        CodeIdeManagerReturnMsg returnMsg = new CodeIdeManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        CodeIdeUserInfo codeIdeUserInfoByUid = mapper.findCodeIdeUserInfoByUid(uid);
        if (codeIdeUserInfoByUid==null){
            log.warn("通过uid查询用户coodeide信息失败!uid:{}",uid);
            Msg.add("通过uid查询用户coodeide信息失败!");
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("查询到用户coodeide信息:{}",codeIdeUserInfoByUid);
        returnMsg.setInfo(codeIdeUserInfoByUid);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public String getNodeIp(String node) {
        log.info("即将通过node名称获取node节点.node:{}",node);
        return mapper.getNodeIp(node);
    }

    @Override
    public List<String> getAllPorts() {
        return mapper.getAllPorts();
    }

}
